<template>
  <div class="shop">
    <h4>购物车</h4>
    <div class="box">
      <div class="div" v-if="lis.length===0">
        <img src="/img/2.png" alt />
        <p>你的购物车空空如也！</p>
      </div>
      <div class="div1" else>
        <div class="cat" v-for="item in lis" :key="item.id">
          <div class="img">
            <img :src="item.pic" alt />
          </div>
          <div class="txt">
            <h4>{{item.name}}</h4>

            <p>
              单价：
              <span>￥{{item.money}}</span>
            </p>
            <div class="btn">
              <span @click="jian(item.id)">-</span>
              <b>{{item.count}}</b>
              <span @click="jia(item.id)">+</span>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="food">
      <p>总计：{{sum.reduce((total,item)=>{
          if(item.count){
            return total+item.count
          }
      },0)}}件</p>
      <p>总价：{{sum.reduce((total,item)=>{
          if(item.count){
            return total+item.count*item.money
          }
      },0)}}元</p>
      <button class="but" @click="tiao">去结算</button>
    </div>
    <router-view />

  </div>
</template>

<script>

import { mapState } from "vuex";
import {mapGetters} from "vuex";
export default {
  components: {

  },
  methods:{
    jia(id){
      this.$store.commit('jia',id)
    },
    jian(id){
      this.$store.commit('jian',id)
    },
    tiao(){

    
        this.$router.push('/zhifu')
      
      
    }
  },

  computed: {
    ...mapState(["lis"]),
    ...mapGetters(["sum"]),
  }
};
</script>

<style lang='scss' scoped>
.shop {
  height: 100%;
  display: flex;
  flex-direction: column;
  h4 {
    height: 50px;
    padding-left: 10px;
    line-height: 50px;
  }
  .box {
    flex: 1;
    display: flex;
    flex-direction: column;
    .div {
      text-align: center;
      margin-top: 20px;
    }
    .div1{
      flex:1;
      overflow-y: auto;
    .cat{
      height: 100px;
      display: flex;
      margin-top: 10px;
      .img{
        width: 100px;
        margin-right: 20px;
      }
      .txt{
        h4{
          padding: 0;
        }
        span{
          color: red
        }
      }
      .btn{
        position: relative;;
        right: -150px;
        bottom: 0;
        span{
          display: inline-block;
          width: 20px;
          height: 20px;
          text-align: center;
          line-height: 20px;
          border:1px solid #542de4;
          border-radius: 50%;
          margin:0 10px;
        }
      }
    }
    }
  }
  .food{
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0px;
    height: 50px;
    line-height: 50px;
    display: flex;
    justify-content: space-around;
    background: rgb(59, 58, 58);
    color: white;
    .but{
      width: 100px;
      background: rgb(47, 255, 82);
      border: none;
      outline: none;
      color: white;
    }
}
}
</style>